package com.springboot.learning.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.springboot.learning.entity.po.User;
import com.springboot.learning.mapper.UserMapper;
import com.springboot.learning.service.UserService;


/**
*
* User 表数据服务层接口实现类
*
*/
@Service
public class UserServiceImpl implements UserService {

	 @Autowired
	 private UserMapper baseMapper;
	 
	 
	 @Autowired
	 private SqlSessionFactory factory;
	 
	@Override
    public int setStatus(Integer userId, int status) {
        return this.baseMapper.setStatus(userId, status);
    }

    @Override
    public int changePwd(Integer userId, String pwd) {
        return this.baseMapper.changePwd(userId, pwd);
    }

    @Override
    public List<Map<String, Object>> selectUsers(String name, String beginTime, String endTime, Integer deptid) {
        return this.baseMapper.selectUsers(name, beginTime, endTime, deptid);
    }

    @Override
    public int setRoles(Integer userId, String roleIds) {
        return this.baseMapper.setRoles(userId, roleIds);
    }

    /**
     * mybatis的一级进行测试，只查询了一次
     * @param id
     * @return
     */
    @Override
    public User selectUserById(Integer userId) {
        // 自动提交事务
        SqlSession sqlSession = factory.openSession(true);
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        Map<String,Object> paramsMap = new HashMap<>(16);
        paramsMap.put("id",userId);
        User rmsUser = userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        userMapper.selectOneUser(paramsMap);
        return rmsUser;
//        return cacheService.selectUserById(id);
    }
    
    @Override
    public User selectUserByAccout(String account) {
    	 Map<String,Object> paramsMap = new HashMap<>(5);
         paramsMap.put("account",account);
         User user = baseMapper.selectOneUser(paramsMap);
         return user;
    }

	@Override
	public void addOrUpdateUser(User user) {
		 // 添加
        if (user!=null && user.getId()!=null && user.getId()!=0) {
        	 Map<String,Object> paramsMap = new HashMap<>(5);
             paramsMap.put("account",user.getAccount());
             User theUser = baseMapper.selectOneUser(paramsMap);
            if (theUser!=null) {
            	//插入
            }
        } else { 
        	//修改
        }
	}


}

